package com.xrz.sxm.aj.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xrz.sxm.aj.adapter.ZixunAdapter;
import com.xrz.sxm.aj.base.BaseAct;
import com.xrz.sxm.aj.base.OnReachBottomListener;
import com.xrz.sxm.aj.entity_tab.DBHelps;

public class ZiXunActivity extends BaseAct implements OnRefreshListener,
		OnReachBottomListener, OnItemClickListener {
	@ViewInject(R.id.pull_refresh_list)
	private PullToRefreshListView mPullToRefreshListView;
	private ListView mListView;
	private ZixunAdapter adapter;
	private LinearLayout mFootView;

	/** 每一页的条数 */
	private static final int PAGESIZE = 10;
	/** 当前页 */
	private int m_currentPage = 1;
	/** 数据全部 */
	private int m_DataCount = 0;
	/** 总页数 */
	private int m_AllPage = 0;
	private boolean isJiaZai = true;

	private DBHelps m_DbHelps;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case 1:
					adapter.clear();
					GetData();
					isJiaZai = false;
					mPullToRefreshListView.onRefreshComplete();
					break;
				case 2:
					GetData();
					isJiaZai = false;
					mPullToRefreshListView.onRefreshComplete();
					break;
			}
		};
	};

	@Override
	protected int setView() {
		return R.layout.layout_zixun;
	}

	@Override
	protected void init() {
		m_DbHelps = new DBHelps(ctx);
		m_DataCount = m_DbHelps.GetZiXunNums();
		m_AllPage = Math.round((m_DataCount + PAGESIZE - 1) / PAGESIZE);
		mFootView = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.footview, null);
		adapter = new ZixunAdapter(this);
//		adapter.setOnReachBottomListener(this);
		mListView = mPullToRefreshListView.getRefreshableView();
//		mListView.addFooterView(mFootView);
		View mHeaderView = LayoutInflater.from(this).inflate(
				R.layout.view_image, null);
		mListView.addHeaderView(mHeaderView);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(this);
		mPullToRefreshListView.setOnRefreshListener(this);
		mPullToRefreshListView.setRefreshing();// 设置进入时就刷新
		handler.sendEmptyMessageDelayed(1, 1000);
	}

	/**
	 * 从数据库获取数据
	 */
	private void GetData() {
		adapter.appendToList(m_DbHelps.getZixunDatas());
		isJiaZai = true;
	}

	@Override
	protected String setTopTitle() {
		return "资讯中心";
	}

	@Override
	protected boolean topHide() {
		return false;
	}

	@Override
	public void onRefresh(PullToRefreshBase refreshView) {
		m_currentPage = 1;
		handler.sendEmptyMessageDelayed(1, 1000);
	}

	@Override
	public void onReachBottom() {
//		if (m_currentPage >= m_AllPage) {
//			isJiaZai = false;
//			mListView.removeFooterView(mFootView);
//			return;
//		}
//		m_currentPage++;
//		handler.sendEmptyMessageDelayed(2, 1000);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (isJiaZai) {
			return;
		}
		if (arg2 >= 2) {
			String ul = adapter.getList().get(arg2 - 2).zxlj.trim();
			Uri uri = Uri.parse(ul);
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			intent.setClassName("com.android.browser",
					"com.android.browser.BrowserActivity");
			this.startActivity(intent);
		}
	}

	/**
	 * 计算从第几条数据开始算起
	 */
	private int getStartPage() {
		int startno = (m_currentPage - 1) * PAGESIZE;
		if (startno < 0)
			startno = 0;
		return startno;
	}
}
